﻿@{
    PageData["Title"] = "Log In";
    
    // Initialize general page variables
    var email = "";
    var password = "";
    var rememberMe = false;

    // Validation
    var isValid = true;
    var emailErrorMessage = "";
    var passwordErrorMessage = "";

    // If this is a POST request, validate and process data
    if (IsPost) {
        email = Request.Form["email"];
        password = Request.Form["password"];
        rememberMe = Request.Form["rememberMe"].AsBool();
        
        // Validate the user's email
        if (email.IsEmpty()) {
            emailErrorMessage = "You must specify an email address.";
            isValid = false;
        }
        
        // Validate the user's password
        if (password.IsEmpty()) {
            passwordErrorMessage = "You must specify a password.";
            isValid = false;
        }
        
        // Confirm there are no validation errors
        if (isValid) {
            // Attempt to login to the Security object using provided creds
            if (WebSecurity.Login(email, password, rememberMe)) {
                Response.Redirect("~/");
            }
            
            // If we arrived here, the login failed; convey that to the user
            isValid = false;
        }    
    }
}

<h2>Log In to Your Account</h2>
<p>
   Please enter your email and password below. If you don't have an account,
   visit the <a href="@Href("~/Account/Register")">registration page</a> and create one.
</p>

@* If one or more validation errors exist, show an error *@
@if (!isValid) {
   <div class="message error icon">There was a problem with your login and/or errors exist in your form.</div>
}

<fieldset class="prettyForm">    
    <form method="post" action="">
        <div>
            <label for="email" id="labelEmail">Email:</label>
            <input type="text" id="email" name="email" value="@email" />
            @* Write any email validation errors to the page *@
            @if (!emailErrorMessage.IsEmpty()) {
                <span class="message error">&raquo; @emailErrorMessage</span>
            }
        </div>         
        <div>
            <label for="password" id="labelPassword">Password:</label>
            <input type="password" id="password" name="password" value="" />
            @* Write any password validation errors to the page *@
            @if (!passwordErrorMessage.IsEmpty()) {
                <span class="message error">&raquo; @passwordErrorMessage</span>
            }
        </div>
        <div>
            <label class="inline-checkbox" for="rememberMe" id="labelRememberMe">Remember Me?</label>
            <input type="checkbox" id="rememberMe" name="rememberMe" value="true"@(rememberMe ? @" checked=""checked""" : "") />
        </div>
        <div>
            <input type="submit" value="Login" />
        </div>
        <div>
            <a href="@Href("~/Account/ForgotPassword")">Did you forget your password?</a>
        </div>
        
        <div class="message info icon">
            <h3>Hint (only for sample purposes):</h3>
            <p>To login as Administrator and manage content with QuickEdit, use the following pre-provisioned account:</p>
            <ul>
                <li><strong>Email:</strong> admin@example.com</li>
                <li><strong>Password:</strong> 123456</li>
            </ul>
        </div>
    </form>
</fieldset>